Repository navigation
Keep simulated Ctrl+C inside the interrupt tests - #2427
Pierre-Yves Le Borgne (pylaterreur) wants to merge 1 commit into
Conversation
|
🎉 All Contributor License Agreements have been signed. Ready to merge. |
There was a problem hiding this comment.
🟡 Changes recommended
The interrupt handler can return while a scheduled interrupt is still pending, allowing a second KeyboardInterrupt to escape into cleanup or pytest and abort the session.
1 open issue
What changed in this PR
This test-only PR addresses intermittent CI session aborts caused by simulated Ctrl+C arriving after blocking Kafka calls return.
Changes:
- Adds a shared interrupt context manager that waits for delayed interrupts and reports premature returns as assertion failures.
- Migrates 23 interrupt-injection sites across the wakeable and share-consumer tests.
- Removes cleanup sleeps, increases two poll timeouts, and removes the producer broker configuration that caused polling to return early.
| File | Description |
|---|---|
tests/test_Wakeable.py |
Adopts the helper and adjusts poll setup, timeouts, and cleanup. |
tests/test_ShareConsumer.py |
Uses the helper for finite and infinite poll interrupt tests. |
tests/common/__init__.py |
Adds the shared delayed-interrupt context manager. |
Review effort: Balanced
| if exc_type is KeyboardInterrupt: | ||
| return False # Interrupted inside the block, as intended. | ||
| # The block ended first. Wait for the interrupt, so that it is raised here, not later. | ||
| try: | ||
| self._sent.wait() | ||
| except KeyboardInterrupt: | ||
| pass |
There was a problem hiding this comment.
Good catch. __exit__ now waits for the scheduled interrupt in every case, including when the block was ended by a different KeyboardInterrupt (a real Ctrl+C); a second KeyboardInterrupt arriving during that wait is swallowed and the original one propagates. The interrupt tests of test_Wakeable.py and test_ShareConsumer.py pass with it (27 passed).
The interrupt tests start a thread that calls _thread.interrupt_main() after a fixed delay, and expect the KeyboardInterrupt inside a try/except around the blocking call. When the call returns first, or the thread wakes up late, the KeyboardInterrupt is raised later: in cleanup, in another test, or in pytest itself, which then aborts the whole session. On the macOS x64 CI agent, pytest stopped after 16 tests of test_Wakeable.py. The 17th, test_can_be_interrupted[producer-poll], never interrupted poll(): with bootstrap.servers set and no broker, poll() returns within a few milliseconds after serving the "connection refused" error. The test only passed because the interrupt landed in the 0.2 s sleep of its finally block, and an interrupt more than about 0.1 s late escaped. Add TestUtils.interrupt_main_after(), a context manager for the blocking call. Leaving the block waits for the interrupt, so it is always raised inside the block, and a call that returns before being interrupted fails with an AssertionError instead. Use it for every simulated Ctrl+C in test_Wakeable.py and test_ShareConsumer.py, drop bootstrap.servers from the producer in test_can_be_interrupted so that poll() blocks until interrupted, and give the two 1 s polls interrupted after 0.4 s the 2 s timeout of the others. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GwUvhmzgfkiZ6vo4uGyesN
93b93c7 to
c363bd4
Compare
|
Thanks for opening this PR. This is a test-only change. If it fixes a failing or flaky CI run, please link an example. |
|
Ankith L (@Ankith-Confluent) here are the CI runs this fixes, both on commits that don't contain this change. In each the macOS x64 job has pytest stopped by
Both are listed under "References" in the description. |

What
The interrupt tests in
tests/test_Wakeable.py, and two intests/test_ShareConsumer.py, start a thread that calls_thread.interrupt_main()after a fixed delay, and expect theKeyboardInterruptinside atry/exceptaround the blocking call. When the call returns first, or the thread wakes up late, theKeyboardInterruptis raised later: in cleanup, in another test, or in pytest itself, which then aborts the whole session. This happened in the first CI run of #2416 on the macOS x64 agent (job): pytest stopped withKeyboardInterruptat<frozen posixpath>:457after 16 tests oftest_Wakeable.py, and the rerun passed.The 17th test,
test_can_be_interrupted[producer-poll], never interruptedpoll(). Its producer hasbootstrap.servers: localhost:9092, and with no broker there,Producer.poll()returns 1 or 2 within a few milliseconds, as soon as it has served the "connection refused" error. The test only passed because the interrupt landed in the 0.2 ssleepof itsfinallyblock; an interrupt more than about 0.1 s late escaped.This adds
TestUtils.interrupt_main_after(delay), a context manager for the blocking call. Leaving the block waits for the interrupt, so theKeyboardInterruptis always raised inside the block; a call that returns before it is interrupted fails withAssertionError: Not interrupted: ...instead. All 23 simulated interrupts use it. The producer intest_can_be_interruptedno longer setsbootstrap.servers, like the producers of the other interrupt tests, sopoll()blocks until it is interrupted, and the sleeps that waited for late interrupts are gone. The two 1 s polls interrupted after 0.4 s get the 2 s timeout of the others, so an interrupt can be up to 1.4 s late everywhere (4.6 s intest_ShareConsumer.py).Checklist
References
KeyboardInterruptat<frozen posixpath>:457after the same 16 tests oftest_Wakeable.py: https://confluentinc.semaphoreci.com/jobs/04d2d52e-aecf-402f-8c9d-6274e3e56078Test & Review
test_can_be_interrupted[producer-poll]on master, traced outside pytest (Python 3.11.16):poll()returned after 1 to 6 ms in all 11 runs, 8 of them under a 10% CPU quota, and the interrupt was always caught by thefinallysleep. On this branch, with only the context manager and the old producer config, the test fails every time withAssertionError: Not interrupted: the block ended before the interrupt sent after 0.1s.TestUtils.send_sigint_after_delay(), running all 27 interrupt tests (Python 3.11.16, Linux x86_64):test_can_be_interrupted[producer-poll]failed in 3, and 1 run aborted withKeyboardInterruptinobj.close()of the next test, after "1 failed, 16 passed", as in CI.poll(timeout=1.0)took 2.51 s intest_calculate_chunk_timeout_utility_function[consumer].AssertionError: Not interrupted: ..., the other 16 pass, and the session runs to the end.test_can_be_interruptedunder a 5% CPU quota with 500 ms periods. Throttled threads wake together, and the interrupting thread, which wakes first, also runs first.test_can_be_interruptedpasses 30 runs out of 30 under the 5% CPU quota.tools/style-format.shon the changed files andflake8pass.Open questions / Follow-ups
poll(timeout=1.0)returning within 2 s. Those bounds can still fail on a loaded machine; this PR does not change them.🤖 Generated with Claude Code
https://claude.ai/code/session_01GwUvhmzgfkiZ6vo4uGyesN